﻿using System;
using System.Web;
using Kiwi.Container;
using Kiwi.Container.Application;
using Kiwi.Container.DependencyInjection;
using Kiwi.Container.Wcf;
using Kiwi.Container.Web;
using SampleWebApplication.code;

namespace SampleWebApplication
{
    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            ContainerManager.Configure(CreateContainer, new AspNetFramework());
        }

        private static IContainer CreateContainer()
        {
            var builder = new ContainerBuilder();

            builder
                .EnablePerHttpSessionContainers()
                .EnablePerHttpRequestContainers()
                .EnableHttpHandlerDependencyInjection(Properties.Unset, true)
                .EnablePerWcfRequestContainers();

            builder.RegisterMany(
                Component.Define(c => HttpContext.Current),
                Component.Define(c => HttpContext.Current.Request),
                Component.Define(c => HttpContext.Current.Response),
                Component.Define(c => HttpContext.Current.Server),
                Component.Define(c => HttpContext.Current.Session),
                Component.Define(c => HttpContext.Current.Application))
                .NoLifetime()
                .PerHttpRequest();

            builder.Register<INotes, ServerNotes>();
            builder.Register<INotes, RequestNotes>()
                .PerHttpRequest();

            return builder.CreateContainer();
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
        }

        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}